﻿using CommonBase;
using DBLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Linq;
using System.Text;

namespace DAL
{
    /// <summary>
    /// 数据操作类，业务逻辑层
    /// </summary>
    public class ISSDal : DataOperation
    {
        #region 常量

        // Region
        public const string QUERY_GET_ALLREGIONDATA = "select * from Region";
        public const string QUERY_GET_ALLREGIONDATA_BYREGIONID = "select * from Region where RegionID = {0}";


        // Site
        public const string QUERY_GET_ALLSITEDATA = "select * from Site";
        public const string QUERY_GET_ALLSITEDATA_BYREGIONID = "select * from Site where RegionID = '{0}'";
        public const string QUERY_GET_ALLSITEDATA_BYSITEID = "select * from Site where SiteID = {0}";


        // SiteEmployeeState
        public const string QUERY_GET_ALLEMPLOYEESTATEDATA_BYSITEID = "select * from SiteEmployeeState where SiteID = '{0}'";


        // Employee
        public const string QUERY_GET_ALLEMPLOYEEDATA = "select * from Employee";
        public const string QUERY_GET_ALLEMPLOYEEDATA_BYEMPID = "select * from Employee where EmpID = '{0}'";

        #endregion

        #region 方法

        #region Region 表操作

        /// <summary>
        /// 修改 Region 项目
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="pictureURL">图片路径</param>
        /// <returns></returns>
        public int EditRegionItem(string id, string pictureURL)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                MessageBox.Show("ID 不为空！", ConstantString.MSG_COMPANY);
                return 0;
            }

            int resultCount = 0;
            DataSet dataSet = new DataSet();

            try
            {
                OleDbDataAdapter oleDbAdapter = GetDataAdapter(string.Format(QUERY_GET_ALLREGIONDATA_BYREGIONID, id),
                    "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName));
                new OleDbCommandBuilder(oleDbAdapter);
                oleDbAdapter.Fill(dataSet, "Region");

                if (dataSet.Tables["Region"].Rows.Count <= 0)
                    return resultCount;

                // 修改数据
                dataSet.Tables["Region"].Rows[0]["RegionURL"] = pictureURL;

                resultCount = oleDbAdapter.Update(dataSet, "Region");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
            finally
            {
                dataSet.Dispose();
            }

            return resultCount;
        }

        #endregion

        #region Site 表操作

        /// <summary>
        /// 批量修改 Site 项目，修改区域站点坐标信息
        /// </summary>
        /// <param name="siteList"></param>
        /// <returns></returns>
        public int EditSiteItem(Dictionary<string, System.Windows.Point> siteList)
        {
            if (siteList == null ||
                (siteList != null && siteList.Count <= 0))
            {
                MessageBox.Show("无数据添加！", ConstantString.MSG_COMPANY);
                return 0;
            }

            int resultCount = 0;
            DataSet dataSet = new DataSet();

            try
            {
                OleDbDataAdapter oleDbAdapter = GetDataAdapter(QUERY_GET_ALLSITEDATA,
                    "db_VWMSConnectionString", string.Format("{0}.exe", Application.ProductName));
                new OleDbCommandBuilder(oleDbAdapter);
                oleDbAdapter.Fill(dataSet, "Site");

                if (dataSet.Tables["Site"].Rows.Count <= 0)
                    return resultCount;

                foreach (KeyValuePair<string, System.Windows.Point> site in siteList)
                {
                    foreach (DataRow siteRow in dataSet.Tables["Site"].Rows)
                    {
                        if (site.Key == siteRow["SiteID"].ToString())
                        {
                            siteRow["X"] = site.Value.X;
                            siteRow["Y"] = site.Value.Y;

                            break;
                        }
                    }
                }

                resultCount = oleDbAdapter.Update(dataSet, "Site");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
            finally
            {
                dataSet.Dispose();
            }

            return resultCount;
        }

        #endregion

        #endregion
    }
}
